package gdconf

import (
	"server_cluster/common/logger"
)

// 道具合成配置表

type ItemComposeConfig struct {
	ItemId       int32    `csv:"ID" gorm:"primary_key;autoIncrement:false"` // 道具合成id
	CostItemList ItemList `csv:"NeedProps,omitempty"`                       // 消耗道具
	CostGold     int32    `csv:"CostCoins,omitempty"`                       // 消耗金币
}

func (i *ItemComposeConfig) TableName() string {
	return "config_item_compose"
}

func (g *GameDataConfig) saveItemComposeConfig() {
	saveTableToDb[ItemComposeConfig](g.Db, readTable[ItemComposeConfig](g.CsvPathPrefix+"ItemCompose.csv"))
}

func (g *GameDataConfig) loadItemComposeConfig() {
	g.GameDataMaps.ItemComposeConfigMap = make(map[int32]*ItemComposeConfig)
	for _, itemComposeConfig := range loadTableFromDb[ItemComposeConfig](g.Db) {
		g.GameDataMaps.ItemComposeConfigMap[itemComposeConfig.ItemId] = itemComposeConfig
	}
	logger.Info("ItemComposeConfig Count: %v", len(g.GameDataMaps.ItemComposeConfigMap))
}

func GetItemComposeConfig(itemId int32) *ItemComposeConfig {
	value, exist := CONF.GameDataMaps.ItemComposeConfigMap[itemId]
	if !exist {
		logger.Error("[ConfigNotFound] ItemComposeConfig, itemId: %v", itemId)
		return nil
	}
	return value
}

func GetItemComposeConfigMap() map[int32]*ItemComposeConfig {
	return CONF.GameDataMaps.ItemComposeConfigMap
}
